package com.huawei.petSystemdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.adapter.PetAdapter;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.data.ShopRepository;
import com.huawei.petSystemdemo.ui.Race.CatActivity;
import com.huawei.petSystemdemo.utils.MyRequest;
import com.huawei.petSystemdemo.utils.OnBindBottomListener;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Shop_Activity extends AppCompatActivity {

    private  String race;
    private int shopId;
    private int current=1;
    private int size=8;

    private TextView shopid_cat;
    private TextView shopid_dog;
    private TextView shopid_smallpet;
    private TextView shopid_all;
    private TextView shopTitle;
    private TextView shop_position;
    Map<String, Object> requestBody = new HashMap<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);

        Intent intent = getIntent();
        shopId = intent.getIntExtra("shopId",-1);
        System.out.println();

        RecyclerView list = findViewById(R.id.shop_list_pet);
        shopTitle = findViewById(R.id.shop_title);
        shop_position = findViewById(R.id.shop_position);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        list.setLayoutManager(manager);

        List<Pets> petsList = new ArrayList<>();

        requestBody.put("shopId" , shopId + "");
        requestBody.put("current", current + "");
        requestBody.put("size", size + "");

        ShopRepository.getByShopId(shopId, new Consumer<Shop>() {
            @Override
            public void accept(Shop shop) {
                shopTitle.setText(shop.getName());
                shop_position.setText(shop.getAdds());
            }
        });





        MyRequest.get("/pet/getByShopId",  requestBody, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("当前无数据");
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                System.out.println(json);
                JSONObject obj = JSON.parseObject(json);
                System.out.println(json);
                //int statusCode = obj.getInteger("statusCode");
                String errMessage = obj.getString("errMessage");
                List<Pets> data = obj.getJSONArray("data").toJavaList(Pets.class);
                //System.out.println(statusCode);
                System.out.println(errMessage);
                System.out.println(data);

                petsList.addAll(data);
            }
        });


        PetAdapter petAdapter = new PetAdapter(petsList, this);
//        petAdapter.setListener(new OnBindBottomListener<List<Pets>>() {
            ///
//            @Override
//            public void bind(List<Pets> list1) {
//                current++;
//                requestBody.put("shopId", shopId + "");
//                requestBody.put("current", current + "");
//                requestBody.put("size", size + "");
//                MyRequest.get("/pet/getRace", requestBody, json -> {
//                    System.out.println(json);
//                    JSONObject obj = JSON.parseObject(json);
//                    int statusCode = obj.getInteger("statusCode");
//                    String errMessage = obj.getString("errMessage");
//                    List<Pets> data = obj.getJSONArray("data").toJavaList(Pets.class);
//                    System.out.println(statusCode);
//                    System.out.println(errMessage);
//                    System.out.println(data);
//
//                    if (data.size() != 0)
//                    {
//                        System.out.println("新数据");
//                        petsList.addAll(data);
//                        petAdapter.notifyDataSetChanged();
//                    }
//                });
//            }
//        });
        list.setAdapter(petAdapter);



        //猫猫馆
        shopid_cat = findViewById(R.id.shopid_cat);
        shopid_cat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestBody.clear();
                requestBody.put("shopId" , shopId+"");
                requestBody.put("current", current + "");
                requestBody.put("size", size + "");
                requestBody.put("race", "猫");

                MyRequest.get("/pet/getByShopIdSort", requestBody, json -> {
                    System.out.println(json);
                    JSONObject obj = JSON.parseObject(json);
                    int statusCode = obj.getInteger("statusCode");
                    String errMessage = obj.getString("errMessage");
                    List<Pets> data = obj.getJSONArray("data").toJavaList(Pets.class);
//要先消除在将data输入到其中
                    petsList.clear();
                    petsList.addAll(data);
                    PetAdapter petAdapter = new PetAdapter(petsList,getBaseContext());
                    list.setAdapter(petAdapter);
                });
            }
        });


        //狗狗馆
        shopid_dog = findViewById(R.id.shopid_dog);
        shopid_dog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestBody.clear();
                requestBody.put("shopId" , shopId+"");
                requestBody.put("current", current + "");
                requestBody.put("size", size + "");
                requestBody.put("race", "狗");

                MyRequest.get("/pet/getByShopIdSort", requestBody, json -> {
                    System.out.println(json);
                    JSONObject obj = JSON.parseObject(json);
                    int statusCode = obj.getInteger("statusCode");
                    String errMessage = obj.getString("errMessage");
                    List<Pets> data = obj.getJSONArray("data").toJavaList(Pets.class);
//要先消除在将data输入到其中
                    petsList.clear();
                    petsList.addAll(data);
                    PetAdapter petAdapter = new PetAdapter(petsList,getBaseContext());
                    list.setAdapter(petAdapter);
                });
            }
        });


        //小宠馆
        shopid_smallpet = findViewById(R.id.shopid_smallpet);
        shopid_smallpet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestBody.clear();
                requestBody.put("shopId" , shopId+"");
                requestBody.put("current", current + "");
                requestBody.put("size", size + "");
                requestBody.put("race", "小宠物");

                MyRequest.get("/pet/getByShopIdSort", requestBody, json -> {
                    System.out.println(json);
                    JSONObject obj = JSON.parseObject(json);
                    int statusCode = obj.getInteger("statusCode");
                    String errMessage = obj.getString("errMessage");
                    List<Pets> data = obj.getJSONArray("data").toJavaList(Pets.class);
//要先消除在将data输入到其中
                    petsList.clear();
                    petsList.addAll(data);
                    PetAdapter petAdapter = new PetAdapter(petsList,getBaseContext());
                    list.setAdapter(petAdapter);
                });
            }
        });

    }
}